package com.newasia.server.common.tool;

import com.newasia.server.dal.entitys.LogPhoneCode;
import com.newasia.server.dal.entitys.LogPhoneCodeExample;
import com.newasia.server.dal.persistence.LogPhoneCodeMapper;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

import java.util.List;
import java.util.Random;

import static com.newasia.server.common.tool.ConstantList.SMS_APPKEY;
import static com.newasia.server.common.tool.ConstantList.SMS_SECRET;
import static com.newasia.server.common.tool.ConstantList.SMS_URL;

/**
 * @author LiChong
 * @description 发送验证码工具类
 * @date 2020/4/7
 */

public class CodePhoneUtils {

    LogPhoneCodeMapper logPhoneCodeMapper = SpringUtil.getBean(LogPhoneCodeMapper.class);

    public static int creatCodePhone(){
        Random r=new Random();
        Integer code = r.nextInt(1000000)+100000;
        return code;
    }

    /**
     * 注册验证码
     *
     * @param phoneNum
     *            发送的手机号码
     * @param code
     *            验证码
     * @return
     */
    public static boolean sendRegistSMS(String phoneNum, Integer code) {

        // 模板编号
        String SmsTemplateCode = "SMS_60230030";

        try {
            TaobaoClient client = new DefaultTaobaoClient(SMS_URL, SMS_APPKEY, SMS_SECRET);
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
            // req.setExtend("123456");
            // 短信类型
            req.setSmsType("normal");
            // 短信抬头
            req.setSmsFreeSignName("远程助手");
            req.setSmsParamString("{\"code\":\"" + code + "\",\"product\":\"远程监控平台\"}");
            req.setRecNum(phoneNum);
            req.setSmsTemplateCode(SmsTemplateCode);
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
        } catch (Exception e) {
            System.err.println("短信发送失败");
        }
        return true;
    }

    /**
     * 发送告警短信（阿里）
     * @param monitor_name
     * @param sid
     * @param phone
     * @param content
     */
    public static boolean sendAlertSMS(String monitor_name, String sid, String phone, String content) {

        // 模板编号
        String SmsTemplateCode = "SMS_118590068";

        TaobaoClient client = new DefaultTaobaoClient(SMS_URL, SMS_APPKEY, SMS_SECRET);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("");
        req.setSmsType("normal");
        req.setSmsFreeSignName("远程监控");
        req.setSmsParamString( "{monitor_name:'"+monitor_name+"',sid:'"+sid+"',content:'"+content+"'}" );
        req.setRecNum(phone);
        req.setSmsTemplateCode(SmsTemplateCode);

        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            return rsp.isSuccess();

        } catch (Exception e) {
            System.err.println("短信发送失败");
            return false;
        }

    }

    /**
     * 发送告警短信（阿里）精简版
     * @param monitor_name
     * @param phone
     * @param content
     */
    public static boolean sendAlertSimpleSMS(String monitor_name, String phone, String content) {
        // 模板编号
        String SmsTemplateCode = "SMS_121000010";

        TaobaoClient client = new DefaultTaobaoClient(SMS_URL, SMS_APPKEY, SMS_SECRET);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("");
        req.setSmsType("normal");
        req.setSmsFreeSignName("远程监控");
        req.setSmsParamString( "{monitor_name:'"+monitor_name+"',content:'"+content+"'}" );
        req.setRecNum(phone);
        req.setSmsTemplateCode(SmsTemplateCode);

        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            return rsp.isSuccess();

        } catch (Exception e) {
            System.err.println("短信发送失败");
            return false;
        }
    }


    /**
     * 发送欠费通知短信（阿里）
     * @param monitor_name
     * @param phone
     * @return
     */
    public static boolean sendArrearageSMS(String monitor_name,String phone) {
        // 模板编号
        String SmsTemplateCode = "SMS_118640101";

        TaobaoClient client = new DefaultTaobaoClient(SMS_URL, SMS_APPKEY, SMS_SECRET);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("");
        req.setSmsType("normal");
        req.setSmsFreeSignName("远程监控");
        req.setSmsParamString( "{monitor_name:'"+monitor_name+"'}" );
        req.setRecNum(phone);
        req.setSmsTemplateCode(SmsTemplateCode);

        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            return rsp.isSuccess();

        } catch (Exception e) {
            System.err.println("短信发送失败");
            return false;
        }
    }
}
